/*
 * WarehouseClient.java
 *
 * $Id: LotClient.java,v 1.3 2009-10-01 10:18:02 mario Exp $
 */
package org.ceteca.explica.client.warehouse;

import java.rmi.AccessException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashSet;

import org.ceteca.explica.client.gui.ExplicaDateSelect;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.ExplicaException;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.RegistryOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.explica.core.warehouse.ProductGroupOT;
import org.ceteca.explica.core.warehouse.LotOT;
import org.ceteca.explica.core.warehouse.ProductOT;
import org.ceteca.explica.core.warehouse.WarehouseManager;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

public class LotClient extends CommonClient {
	/**
     * Message for logging the entity glade configuration file path.
     */
    public static final String EXPLICA_LOG_EDITLOT_GLADEPATH = "The measure unit edition interface configuration file has been found at this path: %s";
    
    private ExplicaDateSelect selectDate;
    
    public LotClient() {
    	super(	LotOT.class,
				InterfaceWarehouse.EXPLICA_EDIT_LOT_GLADE_PATH,
				InterfaceWarehouse.EDIT_LOT_DIALOG_NAME);
    }
	
    @Override
	public ArrayList getEntityFields() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void fillEntityData() {
		this.logger.info("Filling the entity with the selected registry data: " + this.entityEditGuiName);
		if (this.getSelectedEntity() != null && this.entEditView != null) {
			LotOT lot = (LotOT)this.getSelectedEntity();
			// Set up the lot's id
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CODE_WIDGETNAME)).setText(String.format("%d", lot.getIdLot()));
			// Set up the lot's description
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_LOT_DESCRIPTION_WIDGETNAME)).setText(lot.getDescription());
			// Set up the lot's number
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_NUMBER_WIDGETNAME)).setText(lot.getNumber());
			// Set up the lot's quantity
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_QUANTITY_WIDGETNAME)).setText(Converter.getInstance().toString(lot.getQuantity()));
			// Set up the lot's creation date
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).setText(Converter.getInstance().toString(lot.getCreationDate()));
			// Set up the lot's expiration date
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).setText(Converter.getInstance().toString(lot.getExpirationDate()));
			// Set up the lot's product
			if (lot.getProduct() != null) {
				((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME)).setText(lot.getProduct().getInternalValue());
				((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME)).setData(lot.getProduct());
			}
		}
	}

	@Override
	public void clearEntityData() {
		if (this.entEditView != null) {
			// Clear the lot's id
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CODE_WIDGETNAME)).setText("");
			// Clear the lot's description
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_LOT_DESCRIPTION_WIDGETNAME)).setText("");
			// Clear the lot's number
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_NUMBER_WIDGETNAME)).setText("");
			// Clear the lot's quantity
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_QUANTITY_WIDGETNAME)).setText("");
			// Clear the lot's creation date
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).setText("");
			// Clear the lot's expiration date
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).setText("");
			// Clear the lot's product
			((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME)).setText("");
		}
	}

	@Override
	public BaseVO getEntityData() {
		LotOT lot = new LotOT();
		
		if (this.entEditView != null) {
			// Set up the lot's description
			lot.setDescription(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_LOT_DESCRIPTION_WIDGETNAME)).getText());
			// Set up the lot's number
			lot.setNumber(((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_NUMBER_WIDGETNAME)).getText());
			// Set up the lot's creation date
			lot.setCreationDate(Converter.getInstance().parseDate(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).getText()));
			// Set up the lot's expiration date
			lot.setExpirationDate(Converter.getInstance().parseDate(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).getText()));
			// Set up the lot's quantity
			lot.setQuantity(Converter.getInstance().parseBigDecimal(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_QUANTITY_WIDGETNAME)).getText()));

			// Set up the lot's product
			lot.setProduct((ProductOT)((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME)).getData());
		}
		
		if (this.getSelectedEntity() != null) {
			LotOT w = (LotOT)this.getSelectedEntity();
			lot.setVersion(w.getVersion());
			lot.setIdLot(w.getIdLot());
		}
		
		return lot;
	}

	@Override
	public void updateEntityData() {
		if (this.getSelectedEntity() != null && this.entEditView != null) {
			// Set up the lot's description
			((LotOT)this.getSelectedEntity()).setDescription(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_LOT_DESCRIPTION_WIDGETNAME)).getText());
			// Set up the lot's number
			((LotOT)this.getSelectedEntity()).setNumber(((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_NUMBER_WIDGETNAME)).getText());
			// Set up the lot's creation date
			((LotOT)this.getSelectedEntity()).setCreationDate(Converter.getInstance().parseDate(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).getText()));
			// Set up the lot's expiration date
			((LotOT)this.getSelectedEntity()).setExpirationDate(Converter.getInstance().parseDate(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).getText()));
			// Set up the lot's quantity
			((LotOT)this.getSelectedEntity()).setQuantity(Converter.getInstance().parseBigDecimal(
					((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_QUANTITY_WIDGETNAME)).getText()));

			// Set up the lot's product
			((LotOT)this.getSelectedEntity()).setProduct((ProductOT)((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME)).getData());
		}
		
	}

	@Override
	public void setEditionMode(boolean enabled) {
		// Get the lot's code widget
		Control c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_CODE_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's description widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_DESCRIPTION_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's number widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_NUMBER_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's creation date widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's expiration date widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's quantity widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_QUANTITY_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the lot's select product widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_LOT_SELECT_PRODUCT_BUTTON_WIDGETNAME);
		c.setEnabled(enabled);
	}
	
	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildSelectionSets() {
		// Initialize the set of subentity selection widgets
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		
		// Add product group selection button
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceWarehouse.EXPLICA_ENTITY_PRODUCT_ID,
				InterfaceWarehouse.EDIT_LOT_PRODUCT_WIDGETNAME,
				this, (Button)this.getWidget(InterfaceWarehouse.EDIT_LOT_SELECT_PRODUCT_BUTTON_WIDGETNAME)));
	}

	@Override
	public void getWidgets() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init_specific_components() {
		// Connect the signal handler of the select creation date button
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, this.getWidget(InterfaceWarehouse.EDIT_LOT_SELECT_CREATIONDATE_BUTTON_WIDGETNAME),
				"on_select_creationdate_clicked", this);
		
		// Connect the signal handler of the select expiration date button
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, this.getWidget(InterfaceWarehouse.EDIT_LOT_SELECT_EXPIRATIONDATE_BUTTON_WIDGETNAME),
				"on_select_expirationdate_clicked", this);
	}

	@Override
	public void setWidgetsValues(BaseVO entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void on_select_creationdate_clicked(Event e) {
		selectDate = new ExplicaDateSelect(this.logger, this, "on_creationdate_selected");
		selectDate.build();
		selectDate.show(Converter.getInstance().parseDate(
				((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).getText()));
	}
	
	public void on_select_expirationdate_clicked(Event e) {
		selectDate = new ExplicaDateSelect(this.logger, this, "on_expirationdate_selected");
		selectDate.build();
		selectDate.show(Converter.getInstance().parseDate(
				((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).getText()));
	}
	
	public void on_creationdate_selected(Event e) {
		((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_CREATIONDATE_WIDGETNAME)).setText(Converter.getInstance().toString(
				selectDate.getDate()));

	}
	
	public void on_expirationdate_selected(Event e) {
		((Text)this.getWidget(InterfaceWarehouse.EDIT_LOT_EXPIRATIONDATE_WIDGETNAME)).setText(Converter.getInstance().toString(
				selectDate.getDate()));

	}
	
	public String getProductName() {
		if (((LotOT)this.getSelectedEntity()).getProduct() != null) {
			return ((LotOT)this.getSelectedEntity()).getProduct().getInternalValue();
		}
		else
			return "";
	}
	
/*	public String getProductId() {
		if (((LotOT)this.getSelectedEntity()).getProduct() != null) {
			return Converter.getInstance().toString(((LotOT)this.getSelectedEntity()).getProduct().getInternalId());
		}
		else
			return "";
	}
	
	public String getProductReference() {
		if (((LotOT)this.getSelectedEntity()).getProduct() != null) {
			return ((LotOT)this.getSelectedEntity()).getProduct().getReference();
		}
		else
			return "";
	}
	
	public String getProductBarcode() {
		if (((LotOT)this.getSelectedEntity()).getProduct() != null) {
			return ((LotOT)this.getSelectedEntity()).getProduct().getBarcode();
		}
		else
			return "";
	}
*/	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: LotClient.java,v 1.3 2009-10-01 10:18:02 mario Exp $";
	}
}
